#include "i2c.h"
#include "fsl_iomuxc.h"
#include "fsl_lpi2c.h"
#include "clock.h"
#include "fsl_lpi2c.h"
#include "fsl_lpi2c_edma.h"
#include "fsl_edma.h"

#include "fsl_dmamux.h"

#define I2C_BAUDRATE               380000U

#define MASTER_I2C_PERIPH          LPI2C1


//lpi2c_master_handle_t g_m_handle;

//volatile bool g_MasterCompletionFlag = false;

//static void lpi2c_master_callback(LPI2C_Type *base, lpi2c_master_handle_t *handle, status_t status, void *userData)
//{
//    g_MasterCompletionFlag = true;
//    if (status != kStatus_Success)
//    {
//    }
//}


void i2cMasterInit(void)
{
    IOMUXC_SetPinMux( IOMUXC_GPIO_AD_B1_01_LPI2C1_SDA, 1U);
    IOMUXC_SetPinMux( IOMUXC_GPIO_AD_B1_00_LPI2C1_SCL, 1U);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_01_LPI2C1_SDA, 0xD8A0U);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_00_LPI2C1_SCL, 0xD8A0U);

    lpi2c_master_config_t masterConfig;

    /*
     * masterConfig.debugEnable = false;
     * masterConfig.ignoreAck = false;
     * masterConfig.pinConfig = kLPI2C_2PinOpenDrain;
     * masterConfig.baudRate_Hz = 100000U;
     * masterConfig.busIdleTimeout_ns = 0;
     * masterConfig.pinLowTimeout_ns = 0;
     * masterConfig.sdaGlitchFilterWidth_ns = 0;
     * masterConfig.sclGlitchFilterWidth_ns = 0;
     */
    LPI2C_MasterGetDefaultConfig(&masterConfig);
    //masterConfig.pinConfig = kLPI2C_2PinPushPull;
    masterConfig.baudRate_Hz = I2C_BAUDRATE;
    LPI2C_MasterInit(MASTER_I2C_PERIPH, &masterConfig, clockGetLPI2CFrequency());
    //LPI2C_MasterTransferCreateHandle(MASTER_I2C_PERIPH, &g_m_handle, lpi2c_master_callback, NULL);
}


uint32_t i2cMasterWriteByte(uint8_t slaveAddr,uint32_t subAddr,uint8_t value)
{
    status_t reVal = kStatus_Fail;

    lpi2c_master_transfer_t masterXfer = {0};

    masterXfer.slaveAddress   = slaveAddr;
    masterXfer.direction      = kLPI2C_Write;
    masterXfer.subaddress     = (uint32_t)subAddr;
    masterXfer.subaddressSize = 1;
    masterXfer.data           = &value;
    masterXfer.dataSize       = 1 ;
    masterXfer.flags          = kLPI2C_TransferDefaultFlag;

    //reVal = LPI2C_MasterTransferNonBlocking(MASTER_I2C_PERIPH, &g_m_handle, &masterXfer);
    reVal = LPI2C_MasterTransferBlocking(MASTER_I2C_PERIPH,&masterXfer);
    if (reVal != kStatus_Success)
    {
        return -1;
    }
//    while (!g_MasterCompletionFlag)
//    {
//    }
//    g_MasterCompletionFlag = false;
    return 0;
}

uint32_t i2cMasterReadByte(uint8_t slaveAddr,uint32_t subAddr,uint8_t value)
{
    status_t reVal = kStatus_Fail;

    lpi2c_master_transfer_t masterXfer = {0};

    masterXfer.slaveAddress   = slaveAddr;
    masterXfer.direction      = kLPI2C_Read;
    masterXfer.subaddress     = (uint32_t)subAddr;
    masterXfer.subaddressSize = 1;
    masterXfer.data           = &value;
    masterXfer.dataSize       = 1 ;
    masterXfer.flags          = kLPI2C_TransferDefaultFlag;

    //reVal = LPI2C_MasterTransferNonBlocking(MASTER_I2C_PERIPH, &g_m_handle, &masterXfer);
    reVal = LPI2C_MasterTransferBlocking(MASTER_I2C_PERIPH,&masterXfer);
    if (reVal != kStatus_Success)
    {
        return -1;
    }
//    while (!g_MasterCompletionFlag)
//    {
//    }
//    g_MasterCompletionFlag = false;
    return 0;
}


uint32_t i2cMasterWriteBuffer(uint8_t slaveAddr,uint32_t subAddr,uint8_t *buffer,uint32_t bytes)
{
    status_t reVal = kStatus_Fail;

    lpi2c_master_transfer_t masterXfer = {0};

    masterXfer.slaveAddress   = slaveAddr;
    masterXfer.direction      = kLPI2C_Write;
    masterXfer.subaddress     = (uint32_t)subAddr;
    masterXfer.subaddressSize = 1;
    masterXfer.data           = buffer;
    masterXfer.dataSize       = bytes ;
    masterXfer.flags          = kLPI2C_TransferDefaultFlag;

    //reVal = LPI2C_MasterTransferNonBlocking(MASTER_I2C_PERIPH, &g_m_handle, &masterXfer);
    reVal = LPI2C_MasterTransferBlocking(MASTER_I2C_PERIPH,&masterXfer);
    if (reVal != kStatus_Success)
    {
        return -1;
    }
//    while (!g_MasterCompletionFlag)
//    {
//    }
//    g_MasterCompletionFlag = false;
    return 0;
}

uint32_t i2cMasterReadBuffer(uint8_t slaveAddr,uint32_t subAddr,uint8_t *buffer,uint32_t bytes)
{
    status_t reVal = kStatus_Fail;

    lpi2c_master_transfer_t masterXfer = {0};

    masterXfer.slaveAddress   = slaveAddr;
    masterXfer.direction      = kLPI2C_Read;
    masterXfer.subaddress     = (uint32_t)subAddr;
    masterXfer.subaddressSize = 1;
    masterXfer.data           = buffer;
    masterXfer.dataSize       = bytes ;
    masterXfer.flags          = kLPI2C_TransferDefaultFlag;

    //reVal = LPI2C_MasterTransferNonBlocking(MASTER_I2C_PERIPH, &g_m_handle, &masterXfer);
    reVal = LPI2C_MasterTransferBlocking(MASTER_I2C_PERIPH,&masterXfer);
    if (reVal != kStatus_Success)
    {
        return -1;
    }
//    while (!g_MasterCompletionFlag)
//    {
//    }
//    g_MasterCompletionFlag = false;
    return 0;
}
